package com.monas.client.dialog;

import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;

import com.monas.client.database.MonasResourceDatabase;
import com.monas.client.util.UIHelper;

public class AddNodeDialog extends AbstractNodeDialog {

	private static final String MESSAGE = "Please completed node information";

	private List<Object[]> nodes;

	public AddNodeDialog(Shell parent) {
		super(parent, "Add Node Form");
	}

	@Override
	protected Control createContents(Composite parent) {
		Control contents = createNewContents(parent, MESSAGE);

		/*
		 * Dapatkan data user yang sekarang ada di database.
		 */
		try {
			MonasResourceDatabase source = MonasResourceDatabase.getInstance();
			nodes = source.getNodes();
		} catch (Exception e) {
			e.printStackTrace();
		}

		return contents;
	}

	@Override
	protected Control createDialogArea(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		composite.setLayoutData(UIHelper.fillBoth());
		composite.setLayout(new GridLayout(1, false));

		nodeText = createText(composite, "Node Name");
		nodeText.addModifyListener(new ModifyListener() {

			@Override
			public void modifyText(ModifyEvent e) {
				String name = nodeText.getText();
				node.setNode(name);
				checkCompleted();
				if (name.length() > 0) {
					for (Object[] fields : nodes) {
						String node = (String) fields[0];
						if (node.equals(name)) {
							error("Node is exist, please define another node name!");
							return;
						}
					}
				}
			}
		});

		hostText = createText(composite, "Host/IP Address");
		hostText.addModifyListener(new ModifyListener() {

			@Override
			public void modifyText(ModifyEvent e) {
				node.setHost(hostText.getText());
				checkCompleted();
			}
		});

		osText = createText(composite, "Operating System");
		osText.addModifyListener(new ModifyListener() {

			@Override
			public void modifyText(ModifyEvent e) {
				node.setOs(osText.getText());
				checkCompleted();
			}
		});
		
		usernameText = createText(composite, "Username");
		usernameText.addModifyListener(new ModifyListener() {

			@Override
			public void modifyText(ModifyEvent e) {
				node.setUsername(usernameText.getText());
				checkCompleted();
			}
		});
		
		passwordText = createText(composite, "Password");
		passwordText.addModifyListener(new ModifyListener() {

			@Override
			public void modifyText(ModifyEvent e) {
				node.setPassword(passwordText.getText());
				checkCompleted();
			}
		});
		
		endText = createText(composite, "End Key");
		endText.addModifyListener(new ModifyListener() {

			@Override
			public void modifyText(ModifyEvent e) {
				node.setEndLine(endText.getText());
				checkCompleted();
			}
		});
		
		return composite;
	}
}
